package com.sunny.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sunny.model.User;
import com.sunny.query.UserQuery;
import com.sunny.service.IUserService;

import framework.Page;
import framework.base.BaseController;

/**
  * 用户表 [Controller]
  * @ClassName: UserController
  * @Description: 
  * @author baolin.liu  Email:543847778@qq.com
  * @date 2016-08-05 14:08:14
  * @version V1.0
 */
@Controller
@RequestMapping("/sunny/user")
public class UserController extends BaseController
{
	private Log log = LogFactory.getLog(UserController.class);
	
	@Resource
	private IUserService userService;

	/**
	 * 跳转到列表页面
	 * @Title: index
	 * @Description: 
	 * @author baolin.liu 
	 * @date 2016-08-05 14:08:14
	 * @param request
	 * @param response
	 * @return ModelAndView
	 */
	@RequestMapping(value = "/index.do")
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response)
	{
		User user = parseForm(User.class, request);

		ModelAndView view = indexModelAndView("/sunny/user/index");
		view.addObject(INFO, user);

		return view;
	}

	/**
	 * ajax加载数据列表
	 * @Title: list
	 * @Description: 
	 * @author baolin.liu 
	 * @date 2016-08-05 14:08:14
	 * @param request
	 * @param response
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "/list.do")
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request, HttpServletResponse response)
	{
		UserQuery query = parseForm(UserQuery.class, request);

		Page page = new Page(request);
		List<User> list = userService.selectList(query, page);
		int count = userService.selectCount(query);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put(Page.ROWS, list);
		map.put(Page.TOTAL, count);

		return map;
	}

	/**
	 * 跳转到新增页面
	 * @Title: add
	 * @Description: 
	 * @author baolin.liu 
	 * @date 2016-08-05 14:08:14
	 * @param request
	 * @param response
	 * @return ModelAndView
	 */
	@RequestMapping(value = "/add.do")
	public ModelAndView add(HttpServletRequest request, HttpServletResponse response)
	{
		User user = parseForm(User.class, request);
		return infoModelAndView("/sunny/user/add", user);
	}

	/**
	 * 新增对象处理方法
	 * @Title: doAdd
	 * @Description: 
	 * @author baolin.liu 
	 * @date 2016-08-05 14:08:14
	 * @param request
	 * @param response
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "/doAdd.do")
	@ResponseBody
	public Map<String, Object> doAdd(HttpServletRequest request, HttpServletResponse response)
	{
		User user = parseForm(User.class, request);

/*		Long id = nextval(Sequence.SEQ_USER);
		user.setId(id);*/

		Map<String, Object> map = new HashMap<String, Object>();

		try
		{
			userService.insert(user);
			map.put(RESULT, SUCCESS);
		}
		catch (Exception e)
		{
			log.error("", e);
			map.put(RESULT, FAIL);
			map.put(ERROR, e.getMessage());
		}

		return map;
	}

	/**
	 * 查看方法
	 * @Title: doAdd
	 * @Description: 
	 * @author baolin.liu 
	 * @date 2016-08-05 14:08:14
	 * @param request
	 * @param response
	 * @return ModelAndView
	 */
	@RequestMapping(value = "/view.do")
	public ModelAndView view(HttpServletRequest request, HttpServletResponse response)
	{
		User user = parseForm(User.class, request);
		return infoModelAndView("/sunny/user/view", user);
	}

	@RequestMapping(value = "/viewList.do")
	@ResponseBody
	public Map<String, Object> viewList(HttpServletRequest request, HttpServletResponse response)
	{
		UserQuery query = parseForm(UserQuery.class, request);

		List<User> list = userService.selectList(query);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put(Page.ROWS, list);
		map.put(Page.TOTAL, list.size());

		return map;
	}

	/**
	 * 跳转到更新对象页面
	 * @Title: update
	 * @Description: 
	 * @author baolin.liu 
	 * @date 2016-08-05 14:08:14
	 * @param request
	 * @param response
	 * @return ModelAndView
	 */
	@RequestMapping(value = "/update.do")
	public ModelAndView update(HttpServletRequest request, HttpServletResponse response)
	{
		UserQuery query = parseForm(UserQuery.class, request);
		User user = userService.selectOne(query);

		return infoModelAndView("/sunny/user/update", user);
	}

	/**
	 * 执行更新对象操作
	 * @Title: doUpdate
	 * @Description: 
	 * @author baolin.liu 
	 * @date 2016-08-05 14:08:14
	 * @param request
	 * @param response
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "/doUpdate.do")
	@ResponseBody
	public Map<String, Object> doUpdate(HttpServletRequest request, HttpServletResponse response)
	{
		User user = parseForm(User.class, request);
		Map<String, Object> map = new HashMap<String, Object>();

		try
		{
			userService.update(user);
			map.put(RESULT, SUCCESS);
		}
		catch (Exception e)
		{
			log.error("", e);
			map.put(RESULT, FAIL);
			map.put(ERROR, e.getMessage());
		}

		return map;
	}

	/**
	 * 执行删除对象操作
	 * @Title: doDelete
	 * @Description: 
	 * @author baolin.liu 
	 * @date 2016-08-05 14:08:14
	 * @param request
	 * @param response
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "/doDelete.do", headers = { "content-type=application/json;charset=UTF-8" })
	@ResponseBody
	public Map<String, Object> doDelete(HttpServletRequest request, @RequestBody List<Map> list)
	{
		List<User> userList = parseForm(User.class, list);
		Map<String, Object> map = new HashMap<String, Object>();

		try
		{
			userService.delete(userList);
			map.put(RESULT, SUCCESS);
		}
		catch (Exception e)
		{
			log.error("", e);
			map.put(RESULT, FAIL);
			map.put(ERROR, e.getMessage());
		}

		return map;
	}
	
	 
}